package com.jubao.ejb.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.jubao.ejb.dao.CategoryDaoLocal;
import com.jubao.ejb.dao.CategoryDaoRemote;
import com.jubao.ejb.dao.interceptor.TracingInterceptor;
import com.jubao.ejb.dao.util.DaoConstant;
import com.jubao.ejb.entity.Category;

/**
 * @author conan
 * 
 */
@Stateless
@Local( { CategoryDaoLocal.class })
@LocalBinding(jndiBinding = DaoConstant.CategoryDaoLocalJndiName)
@Remote( { CategoryDaoRemote.class })
@RemoteBinding(jndiBinding = DaoConstant.CategoryDaoRemoteJndiName)
@Interceptors( { TracingInterceptor.class })
public class CategoryDaoImpl implements CategoryDaoLocal, CategoryDaoRemote {
	static Logger logger = Logger.getLogger(CategoryDaoImpl.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = 5851122982667218379L;

	@PersistenceContext(unitName = "default")
	protected EntityManager em;

	public Category save(Category category) {
		return em.merge(category);
	}

	public Category find(String categoryId) {
		Query q = em.createNamedQuery("CategoryDao.eql.findCategoryById").setParameter("categoryId", categoryId)
				.setMaxResults(1).setHint("org.hibernate.cacheable", new Boolean(true));
		//.setHint("org.hibernate.cacheRegion", "/entity/Category/Single")
		List<Category> categorys = q.getResultList();
		Category result = null;
		if (categorys != null && categorys.size() > 0) {
			result = categorys.get(0);
		}
		return result;
	}

	public Category find(long categorySysId) {
		return em.find(Category.class, categorySysId);
	}

	public List<Category> getPrimaryCategoryTrail(String categoryId) {
		List<Category> result = null;

		Category category = this.find(categoryId);
		if (category != null) {
			result = getPrimaryCategoryTrail(category.getSysId());
		}
		return result;
	}

	public List<Category> getPrimaryCategoryTrail(long categorySysId) {

		List<Category> result = new ArrayList<Category>(0);
		Category pg = em.find(Category.class, categorySysId);
		if (pg != null && pg.getPrimaryParent() != null) {
			result = getPrimaryCategoryTrail(pg.getPrimaryParent().getSysId());
		}
		if (pg != null) {
			result.add(pg);
		}
		return result;
	}

	public List<Category> getChildCategoryList(String parentCategoryId, int pgno, int pgsize, String orderBy) {
		// TODO seems use treecache, it will be slowdown?????
		List<Category> categorys = null;
		if (parentCategoryId != null) {
			Query q = em.createNamedQuery("CategoryDao.eql.getChildCategoryListByParentId").setParameter(
					"parentCategoryId", parentCategoryId).setHint("org.hibernate.cacheable", new Boolean(true));
			//.setHint(			"org.hibernate.cacheRegion", "/entity/Category/ChildCategory")

			categorys = q.setFirstResult(pgno * pgsize).setMaxResults(pgsize).getResultList();
		}
		return categorys;
	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.dao.CategoryDao#getChildCategoryList(long, int, int, java.lang.String)
	 */
	public List<Category> getChildCategoryList(long parentCategorySysId, int pgno, int pgsize, String orderBy) {
		List<Category> categorys = null;
		if (parentCategorySysId > -1) {
			Query q = em.createNamedQuery("CategoryDao.eql.getChildCategoryListByParentSysId").setParameter(
					"parentCategorySysId", parentCategorySysId).setHint("org.hibernate.cacheable", new Boolean(true));
			//.setHint(			"org.hibernate.cacheRegion", "/entity/Category/ChildCategory")

			categorys = q.setFirstResult(pgno * pgsize).setMaxResults(pgsize).getResultList();
		}
		return categorys;
	}

	public void setEm(EntityManager em) {
		this.em = em;
	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.dao.CategoryDao#getChildCategoryCount(java.lang.String)
	 */
	public long getChildCategoryCount(String parentCategoryId) {
		Category cat = this.find(parentCategoryId);
		if (cat != null)
			return getChildCategoryCount(cat.getSysId());
		return 0;
	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.dao.CategoryDao#getChildCategoryCount(long)
	 */
	public long getChildCategoryCount(long parentCategorySysId) {
		long count = 0;
		if (parentCategorySysId > -1) {
			Query q = em.createNamedQuery("CategoryDao.eql.getChildCategoryCountByParentSysId").setParameter(
					"parentCategorySysId", parentCategorySysId).setHint("org.hibernate.cacheable", new Boolean(true));
			//.setHint(			"org.hibernate.cacheRegion", "/entity/Category/ChildCategory")

			count = (Long) q.getSingleResult();
		}
		return count;
	}

}
